package com.syn.synold4s.control;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.ListPopupWindow;

import com.syn.synold4s.R;
import com.syn.synold4s.adapter.PopupShortAdapter;
import com.syn.synold4s.bean.dto.ShortDto;

import java.util.ArrayList;
import java.util.List;

public class ShortPopupWindow extends ListPopupWindow {

    private PopupShortAdapter mAdapter;
    private List<ShortDto> mData;
    private Context mContext;

    public void setData(List<ShortDto> data) {
        mData.clear();
        mData.addAll(data);
        mAdapter.notifyDataSetChanged();
    }

    public void setData(List<ShortDto> data, String selectCodes) {
        if (data == null) {
            return;
        }
        mData.clear();
        mData.addAll(data);
        if (TextUtils.isEmpty(selectCodes)) {
            for (int i = 0; i < mData.size(); i++) {
                mData.get(i).setSelect(true);
            }
        }else {
            for (int i = 0; i < mData.size(); i++) {
                mData.get(i).setSelect(selectCodes.contains(mData.get(i).getCode()));
            }
        }
        mAdapter.notifyDataSetChanged();
    }

    public void changeState(int position) {
        mData.get(position).setSelect(!mData.get(position).getSelect());
        mAdapter.notifyDataSetChanged();
    }

    public ShortDto getSelectDto(int position) {
        return mData.get(position);
    }

    public String[] getSelectIdsAndNames() {
        String[] data = new String[3];
        StringBuilder names = new StringBuilder();
        StringBuilder codes = new StringBuilder();
        boolean isAll = true;
        for (ShortDto dto : mData) {
            if (dto.getSelect()) {
                names.append(",").append(dto.getName());
                codes.append(",").append(dto.getCode());
            } else {
                isAll = false;
            }
        }
        data[0] = isAll ? mContext.getString(R.string.all) : names.length() > 0 ? names.substring(1) : "";
        data[1] = codes.length() > 0 ? codes.substring(1) : "";
        return data;
    }

    public ShortPopupWindow(@NonNull Context context) {
        super(context);
        init(context);
    }

    public ShortPopupWindow(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ShortPopupWindow(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public ShortPopupWindow(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    @SuppressLint("RestrictedApi")
    private void init(Context context) {
        mContext = context;
        mData = new ArrayList<>();
        mAdapter = new PopupShortAdapter(context, mData);
        setDropDownGravity(Gravity.START);
        setAdapter(mAdapter);
        setDropDownAlwaysVisible(false);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        setPromptPosition(ListPopupWindow.POSITION_PROMPT_BELOW);
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
    }
}
